﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

namespace IotherRis
{
    public class LoadProcess
    {
        /// <summary>
        /// 检查当前进程是否已存在  
        /// </summary>
        /// <param name="p_Message">发送进程消息</param>
        ///  <param name="p_IsPath">通过路径判断 false是通过进程名判断</param>
        /// <returns>true不存在  false存在</returns>
        public static bool StarPoint(string p_Message, bool p_IsPath)
        {
            Process _Process = Process.GetCurrentProcess();
            Process[] _ProcessList = Process.GetProcessesByName(_Process.ProcessName);
            int _ProcessInst = 0;
            IntPtr _ProcessMainWindowHandle = IntPtr.Zero;
            if (p_IsPath)
            {
                for (int i = 0; i != _ProcessList.Length; i++)
                {
                    if (_ProcessList[i].MainModule.FileName == _Process.MainModule.FileName) _ProcessInst++;

                    if (_ProcessList[i].Id != _Process.Id) _ProcessMainWindowHandle = _ProcessList[i].MainWindowHandle;
                }
            }
            else
            {
                _ProcessInst = _ProcessList.Length;
            }
            if (_ProcessInst == 1)
            {
                return true;
            }
            else
            {
                if (p_Message != null) SendMessage(p_Message, _ProcessMainWindowHandle);
                return false;
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="Message">发送消息</param>
        /// <param name="p_MainIntPtr">主窗体</param>
        private static void SendMessage(string Message, IntPtr p_MainIntPtr)
        {
            byte[] _Bytes = System.Text.Encoding.Default.GetBytes(Message);
            COPYDATASTRUCT _Data = new COPYDATASTRUCT();
            _Data.dwData = new IntPtr(100);
            _Data.lpData = Message;
            _Data.cbData = _Bytes.Length + 1;
            SendMessage(p_MainIntPtr, 0x004A, 0, ref _Data);
        }

        /// <summary>
        /// 获取消息
        /// </summary>
        public class LoadProcessMessage : NativeWindow
        {
            public LoadProcessMessage(IntPtr p_MainWindowHandle)
            {
                base.AssignHandle(p_MainWindowHandle);
            }
            public delegate void GetMessage(string Text);
            public event GetMessage LoadMessage;

            protected override void WndProc(ref Message m)
            {
                if (m.Msg == 0x004A)
                {
                    if (LoadMessage != null)
                    {
                        COPYDATASTRUCT _Data = new COPYDATASTRUCT();
                        Type _DataType = _Data.GetType();
                        _Data = (COPYDATASTRUCT)m.GetLParam(_DataType);
                        LoadMessage(_Data.lpData);
                    }

                }
                base.WndProc(ref m);
            }
        }

        /// <summary>
        /// 消息
        /// </summary>
        public struct COPYDATASTRUCT
        {
            /// <summary>
            /// 句柄 (IntPtr) 100
            /// </summary>
            public IntPtr dwData;
            /// <summary>
            /// 数据大小  是byte的大小
            /// </summary>
            public int cbData;
            /// <summary>
            /// 数据
            /// </summary>
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, ref COPYDATASTRUCT copyData);

    }
}
